/****************************************************
          File Name:rmdir.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Mon Dec  8 20:31:43 2014
*****************************************************/

#include "dir.h"

int main(int argc , char *argv[])
{
    int res;

    if(argc < 2)
    {
        fprintf(stderr, "%s dir\n", argv[0]);
        exit(1);
    }

    while(1)
    {
        if(chdir(argv[1]) == -1)
        {
            if(errno == ENOENT)
            {
                printf("now, in the deepest path!\n");
                break;
            }
            else
                err("chdir");
        }
    }

    printf("now remove dirs\n");

    while(1)
    {
        if(chdir("../") == -1)
            perror("chdir in while");
        if(rmdir(argv[1]) == -1)
        {
            if(errno == ENOTEMPTY)
                continue;
            else if(errno == ENOENT)
            {
                printf("all dirs have been removed\n");
                break;
            }
            else
                err("rmdir"); 
        }
    }

    return 0;
}
